﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Security.Principal;
using GDoc.Utils;
using GDoc.Domain.Services;

namespace GDoc.Web.Controllers
{
    public class _ControllerBase : Controller
    {
        public _ControllerBase()
        {
            ViewBag.SelectedMenu = "home";
        }

        protected Domain.User GetCurrentUser()
        {
            if (!HttpContext.Request.IsAuthenticated)
                return null;

            GDocPrincipal principal = HttpContext.User as GDocPrincipal;

            if (principal == null)
                principal = GDocPrincipal.BuildPrincipal(HttpContext);

            HttpContext.User = principal;

            return principal.GIdentity.User;
        }

        protected Domain.Account GetCurrentAccount()
        {
            if (!HttpContext.Request.IsAuthenticated)
                return null;

            GDocPrincipal principal = HttpContext.User as GDocPrincipal;

            if (principal == null)
                principal = GDocPrincipal.BuildPrincipal(HttpContext);

            HttpContext.User = principal;

            return principal.GIdentity.Account;
        }

        protected bool HasAccountSelected
        {
            get
            {
                return (GetCurrentAccount() != null);
            }
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
        }
    }
}
